$(function () {
    'use strict';
    
    var contextPath = $('#contextPath').val();
    var imgPath = $('#imgPath').val();

    // Start slideshow button:
    $('#start-slideshow').button().click(function () {
        var options = $(this).data();
        var modal = $(options.target);
        var data = modal.data('modal');
        if (data) {
            $.extend(data.options, options);
        } else {
            options = $.extend(modal.data(), options);
        }
        modal.find('.modal-slideshow').find('i')
            .removeClass('icon-play')
            .addClass('icon-pause');
        modal.modal(options);
    });

    // Toggle fullscreen button:
    $('#toggle-fullscreen').button().click(function () {
        var button = $(this),
            root = document.documentElement;
        if (!button.hasClass('active')) {
            $('#modal-gallery').addClass('modal-fullscreen');
            if (root.webkitRequestFullScreen) {
                root.webkitRequestFullScreen(
                    window.Element.ALLOW_KEYBOARD_INPUT
                );
            } else if (root.mozRequestFullScreen) {
                root.mozRequestFullScreen();
            }
        } else {
            $('#modal-gallery').removeClass('modal-fullscreen');
            (document.webkitCancelFullScreen ||
                document.mozCancelFullScreen ||
                $.noop).apply(document);
        }
    });
    $.get(contextPath+"gallery/imglist", function(imgs){
    	var gallery = $('#gallery');
    	$.each(imgs, function (index, img) {
            var url = imgPath + img.filename;
            var s_url = imgPath + 's_' + img.filename;
            $('<a rel="gallery"/>')
                .append($('<img>').prop('src', s_url))
                .prop('href', url)
                .prop('title', img.title)
                .appendTo(gallery);
        });
    });
});